package Util;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.Part;
import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class FileUtil {

    public static Boolean savaFile(HttpServletRequest request, String fileName) {
        boolean flag = false;
        //获取文件存放的真实的目录
        String realPath = request.getServletContext().getRealPath("/file");
        String savePath = request.getParameter("savePath");
        realPath = realPath + "/" + savePath;
        // 判断 存放的目录是否存在 如果目录不存在则创建
        File uploadDir = new File(realPath);
        if (!uploadDir.exists()) {
            uploadDir.mkdir();
        }

        //创建文件上传工厂
        DiskFileItemFactory factory = new DiskFileItemFactory();
        //创建文件上传组件
        ServletFileUpload upload = new ServletFileUpload(factory);

        try {
            //获取所有文件
            List<FileItem> items = upload.parseRequest(request);
            for (FileItem i : items) {
                //判断是否是文件

                System.out.println(i.getName());
                System.out.println("两次文件名字的对比：");
                System.out.println(fileName.equals(i.getName()));
                // File f = new File(realPath + "/" + i.getName());
                //  i.write(f);

            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }


    /**
     * 读取Excel表格的内容
     *
     * @param file 要读取的文件
     * @return 读取的内容  ArrayList<ArrayList<String>>
     */
    public static ArrayList<ArrayList<String>> readExcel(File file) {
        ArrayList<ArrayList<String>> aa = new ArrayList<>();
        try {
            Workbook workbook = Workbook.getWorkbook(new FileInputStream(file));
            Sheet sheetA = workbook.getSheet(0);
            for (int i = 1; i < sheetA.getRows(); i++) {
                ArrayList<String> a = new ArrayList<>();
                for (int j = 0; j < sheetA.getColumns(); j++) {
                    Cell cell = sheetA.getCell(j, i);
                    a.add(cell.getContents());
                }
                aa.add(a);
            }
            //读取文件后删除文件
            // file.delete();
            return aa;
        } catch (Exception e) {
            return aa;
        }
    }


    public static String getFileName(Part part) {
        String header = part.getHeader("Content-Disposition");
        String fileName;
        /*
         * String[] tempArr1 = header.split(";");代码执行完之后，在不同的浏览器下，tempArr1数组里面的内容稍有区别
         * 火狐或者google浏览器下：tempArr1={form-data,name="file",filename="snmp4j--api.zip"}
         * IE浏览器下：tempArr1={form-data,name="file",filename="E:\snmp4j--api.zip"}
         */
        String[] tempArr1 = header.split(";");
        /*
         *火狐或者google浏览器下：tempArr2={filename,"snmp4j--api.zip"}
         *IE浏览器下：tempArr2={filename,"E:\snmp4j--api.zip"}
         */
        String[] tempArr2 = tempArr1[2].split("=");
        //获取文件名，兼容各种浏览器的写法
        fileName = tempArr2[1].substring(tempArr2[1].lastIndexOf("\\") + 1).replaceAll("\"", "");
        String uuid = UUID.randomUUID().toString();
        return uuid + fileName;
    }


}
